package com.biz.primus.ms.oms.trans;

import com.biz.primus.common.utils.CopyUtils;
import com.biz.primus.model.oms.enums.CancelType;
import com.biz.primus.model.oms.vo.OmsConsigneeVo;
import com.biz.primus.model.oms.vo.OmsItemVo;
import com.biz.primus.model.oms.vo.OmsOrderRespVo;
import com.biz.primus.model.oms.vo.OmsOrderVo;
import com.biz.primus.ms.base.service.IdService;
import com.biz.primus.ms.oms.dao.po.OmsConsignee;
import com.biz.primus.ms.oms.dao.po.OmsOrder;
import com.biz.primus.ms.oms.dao.po.OmsOrderItem;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yanweijin
 * @date 2017/11/17
 */
@Component
public class OrderConverter {

	@Autowired
	private IdService idService;

	@Autowired
	private OrderItemListConverter itemListConverter;

	@Autowired
	private ConsigneeConverter consigneeConverter;

	public OmsOrder vo2po(OmsOrderVo vo){
		OmsOrder order = new OmsOrder();
		order.setOrderSource(vo.getOrderSource());
		order.setOrderCode(vo.getCode());
		order.setId(idService.getNextId());
		order.setUserId(vo.getUserId());
		order.setChannel(vo.getChannel());
		order.setState(vo.getState());
		order.setDeliveryType(vo.getDeliveryType());
		order.setDeliveryState(vo.getDeliveryState());
		order.setDepotCode(vo.getDepot());
		order.setDiscountAmount(vo.getDiscountAmount());
		order.setOrderSource(vo.getOrderSource());
		order.setExpectedReceivingTime(order.getExpectedReceivingTime());
		order.setFreightAmount(vo.getFreightAmount());
		order.setInAfterSale(false);
		order.setOrderAmount(vo.getAmount());
		order.setRemark(vo.getRemark());
		order.setSelfRemark(vo.getSelfRemark());
		order.setCancelType(CancelType.none);
		order.setPaidAmount(vo.getLiquidated());
		order.setSaleTime(vo.getSaleTime());
		order.setFreightAmount(vo.getFreightAmount());
		List<OmsOrderItem> items = itemListConverter.toPoList(vo.getItems(), order);
		order.setItems(items);
		order.setOriginData(vo);
		OmsConsignee consignee = consigneeConverter.vo2po(vo.getConsignee(), order.getId());
		order.setConsignee(consignee);
		return order;
	}

	public OmsOrderVo po2vo(OmsOrder order){
		OmsOrderVo vo = order.getOriginData();
		vo.setState(order.getState());
		vo.setCode(order.getOrderCode());
		vo.setType("orders");
		vo.setDeliveryType(order.getDeliveryType());
		vo.setDeliveryState(order.getDeliveryState());
		vo.setDepot(order.getDepotCode());
		vo.setDiscountAmount(order.getDiscountAmount());
		vo.setOrderSource(order.getOrderSource());
		vo.setExpectedReceivingTime(order.getExpectedReceivingTime());
		vo.setFreightAmount(order.getFreightAmount());
		vo.setAmount(order.getOrderAmount());
		vo.setRemark(order.getRemark());
		vo.setSelfRemark(order.getSelfRemark());
		OmsConsigneeVo consigneeVo = consigneeConverter.po2vo(order.getConsignee());
		vo.setConsignee(consigneeVo);
		return vo;
	}

	public OmsOrderRespVo po2RespVo(OmsOrder omsOrder){
		OmsOrderRespVo omsOrderRespVo = new OmsOrderRespVo();
		CopyUtils.copyProperties(omsOrder,omsOrderRespVo,"items");
		List<OmsItemVo> omsItemVos = Lists.newArrayList();
		omsOrder.getItems().stream().map(req ->{
			OmsItemVo omsItemVo=new OmsItemVo();
			CopyUtils.copyProperties(req,omsItemVo);
			return Lists.newArrayList(omsItemVo);
		}).collect(Collectors.toList()).forEach(omsItemVos::addAll);
		omsOrderRespVo.setItems(omsItemVos);
		OmsConsigneeVo consigneeVo = consigneeConverter.po2vo(omsOrder.getConsignee());
		omsOrderRespVo.setConsignee(consigneeVo);
		return omsOrderRespVo;
	}


}
